package day1110_3;

import java.io.*;
class Rectangle  implements   Serializable {
    int length;
    transient    int width;
}
public class W2 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //用ObjectOutputStream把对象保存到obj.txt
        ObjectOutputStream oos=//1.
            new ObjectOutputStream(new FileOutputStream("D:\\obj.txt"));

        Rectangle  cfx=new Rectangle();   cfx.length=10;  cfx.width=8;
        oos.writeObject(cfx);//2.

        oos.close();
        //在这里用ObjectInputStream的readObject方法读出对象并打印两个属性
        ObjectInputStream  ois=
                new ObjectInputStream(new FileInputStream("D:\\obj.txt"));

        Rectangle rect=(Rectangle) ois.readObject();
        System.out.println(rect.length  +","+ rect.width);

        ois.close();
    }
}//运行结果会将Rectangle对象中的length值读出来，width读取不出来(因为width添加了transient不保存)



